// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.chips;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class Incrementor2 implements Chip {
  public final IntBus ADDRESS = new IntBus();
  public final ByteBus DATA = new ByteBus();
  public final Pin WR = new Pin();
  public final Pin REQ = new Pin();
  
  private int address = 0;
  private boolean read = false;
  private int counter;
  
  public Incrementor2(boolean runFirst) {
    counter = runFirst ? 0 : 2;
  }
  
  public void update() {
    if ((counter++ & 0x02) == 0) {
      if (read) {
        ADDRESS.isActive = true;
        ADDRESS.value = address;
        
        DATA.isActive = false;
        
        WR.isActive = true;
        WR.value = false;
        
        REQ.isActive = true;
        REQ.value = true;
      } else {
        ADDRESS.isActive = true;
        ADDRESS.value = address++;
        
        DATA.isActive = true;
        DATA.value++;

        WR.isActive = true;
        WR.value = true;
        
        REQ.isActive = true;
        REQ.value = true;
      }
      read = !read;
    } else {
      ADDRESS.isActive = false;
      DATA.isActive = false;
      WR.isActive = false;
      REQ.isActive = false;
    }
  }
}
